package com.nageoffer.jobtrain.judge.mq.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.nageoffer.jobtrain.common.rocketmq.base.MessageWrapper;
import com.nageoffer.jobtrain.judge.common.constant.RocketMQContent;
import com.nageoffer.jobtrain.judge.mq.event.OjQuestionSubmitEvent;
import com.nageoffer.jobtrain.judge.service.OjQuestionJudgeService;
import com.nageoffer.jobtrain.judge.service.dto.OjQuestionSubmitJudgeDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * 预约竞赛即将开始提醒用户
 */
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(
        topic = RocketMQContent.USER_OJ_QUESTION_SUBMIT_TOPIC_KEY,
        consumerGroup = RocketMQContent.USER_OJ_QUESTION_SUBMIT_CG_KEY
)
@Slf4j(topic = "OjQuestionSubmitConsumer")
public class OjQuestionSubmitConsumer implements RocketMQListener<MessageWrapper<OjQuestionSubmitEvent>> {
    private final OjQuestionJudgeService ojQuestionJudgeService;

    @Override
    public void onMessage(MessageWrapper<OjQuestionSubmitEvent> messageWrapper) {
        // 开头打印日志，平常可 Debug 看任务参数，线上可报平安（比如消息是否消费，重新投递时获取参数等）
        log.info("[消费者] 用户提交代码判题事件 - 执行消费逻辑，消息体：{}", JSON.toJSONString(messageWrapper));

        // 将消息转化为判题逻辑DTO进行判题服务
        ojQuestionJudgeService.ojQuestionJudge(BeanUtil.toBean(messageWrapper.getMessage(), OjQuestionSubmitJudgeDTO.class));
    }
}